package ch9.dongtai;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

import ch9.Goods;

public class SchoolHandler implements InvocationHandler {
	private Object target;

	public SchoolHandler(Object o) {
		this.target = o;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("在" + new java.util.Date() + ",调用了"
				+ method.getName() + ",参数是" + Arrays.toString(args));
		if (method.getName().equals("send")) {
			Goods g = (Goods) args[0];
			if (g.getName().equals("钻石"))
				g.setName("玻璃");
		}
		Object o = method.invoke(target, args);
		return o;
	}

	public Object getProxy() {
		// 动态返回一个代理对象
		return Proxy.newProxyInstance(target.getClass().getClassLoader(),
				target.getClass().getInterfaces(), this);
	}
}
